/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.core.dashboard;

import java.io.IOException;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JFrame;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mopore.jat.gui.Theme;
import org.mopore.jat.gui.WindowController;
import org.mopore.jat.plugin.IPluginFileAccess;
import org.mopore.jat.plugin.PluginException;

/**
 * @author Jens Nixdorf
 *
 */
public class Dashboard implements IDashboard {

	
	private final Log log = LogFactory.getLog(getClass().getName());
	
	private final IPluginFileAccess fileAccess;

	private final WindowController windowController;
	
	private DashboardContainer container;

	public Dashboard( IPluginFileAccess fileAccess, WindowController windowController  ) {
		this.fileAccess = fileAccess;
		this.windowController = windowController;
		container = null;
	}
	
	
	public void pin( DashboardInfo info, String pinnerId ) {
		assertContainer();
		DashboardInfoHelper[] helpers = 
			container.getDashboardInfoHelpers();
		
		int size = helpers.length;
		
		DashboardInfoHelper[] newHelpers = new DashboardInfoHelper[ size + 1 ]; 
		System.arraycopy(helpers, 0, newHelpers, 0, size );
		
		DashboardInfoHelper helper = new DashboardInfoHelper( info );
		newHelpers[ size ] = helper;
		
		container.setDashboardInfoHelpers( newHelpers );
		try {
			fileAccess.persist( container );
		} catch (IOException e) {
			String error = "Can not persist dashboard!";
			log.error( error,  e );
			throw new RuntimeException( error, e );
		}
	}


	/**
	 * 
	 */
	public void show( Theme theme ) {
		
		assertContainer();
		DashboardInfoHelper[] helpers = container.getDashboardInfoHelpers();
		List<DashboardInfo> infoList = new LinkedList<DashboardInfo>();
		for (DashboardInfoHelper helper : helpers) {
			DashboardInfo info = helper.toDashboardInfo();
			infoList.add( info );
		}
		
		DashboardWizardInfo wizardInfo = new DashboardWizardInfo( infoList );
		
		try {
			JFrame frame = new DashboardWizardDialog( wizardInfo, theme );
			windowController.askToShow( frame, true );
		} catch (PluginException e) {
			String errorMessage = "error showing dashboard";
			log.error( errorMessage, e );
			throw new RuntimeException( errorMessage, e );
		}
		
	}
	
	
	private void assertContainer() {
		if ( container == null ) {
			try {
				container = fileAccess.retrieve( DashboardContainer.class );
			} catch (IOException e) {
				String error = "Can not retrieve dashboard!";
				log.error( error,  e );
				throw new RuntimeException( error, e );
			}
		}
		
		Calendar now = Calendar.getInstance();
		long nowTime = now.getTimeInMillis();
		
		List<DashboardInfoHelper> helperList = new LinkedList<DashboardInfoHelper>();
		DashboardInfoHelper[] helpers = container.getDashboardInfoHelpers();
		boolean changed = false;
		for (DashboardInfoHelper helper : helpers) {
			long helperTime = helper.getValidTill();
			if ( nowTime < helperTime ) {
				helperList.add( helper );
			}
			else {
				changed = true;
			}
		}
		if ( changed ) {
			helpers = helperList.toArray( new DashboardInfoHelper[0] );
			container.setDashboardInfoHelpers( helpers );
			try {
				fileAccess.persist( container );
			} catch (IOException e) {
				String error = "Can not persist dashboard!";
				log.error( error,  e );
				throw new RuntimeException( error, e );
			}
		}
		
	}
	
}
